import { createWebHistory, createRouter } from 'vue-router'

import routes from './routes'
import { useUserStore } from '@/store/user'
const router = createRouter({
  history: createWebHistory(),
  routes,
})

// 路由守卫
// 导入仓库
let store = null
router.beforeEach((to, from, next) => {
  // 去home 需要校验
  if (to.path == '/home') {
    // 获取仓库
    store = useUserStore()
    if (store.token) {
      next()
    } else {
      next('/')
    }
  } else {
    next()
  }
})

export { router }
